package com.jwsoft.manager.core.integration.bigScreen.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.module.base.common.vo.common.CommonVO;
import com.bifang.module.base.core.integration.CommonIntegration;
import com.bifang.module.base.core.integration.DictIntegration;
import com.jwsoft.manager.common.vo.bigScreen.analysisXq.StuEnrollDgxxInVO;
import com.jwsoft.manager.common.vo.bigScreen.analysisXq.StuEnrollXxtjVO;
import com.jwsoft.manager.common.vo.bigScreen.analysisYbm.*;
import com.jwsoft.manager.common.vo.eduAddress.EduAddressVO;
import com.jwsoft.manager.common.vo.eduAddress.SchoolAddressQueryVO;
import com.jwsoft.manager.common.vo.eduSchool.EduSchoolVO;
import com.jwsoft.manager.core.dao.model.EduSchoolConfig;
import com.jwsoft.manager.core.dao.model.EduSchoolTj;
import com.jwsoft.manager.core.dao.model.EduSjtj03;
import com.jwsoft.manager.core.dao.model.EduStudentEnroll;
import com.jwsoft.manager.core.dao.service.*;
import com.jwsoft.manager.core.integration.bigScreen.AnalysisXwtjIntegration;
import com.jwsoft.manager.core.util.EduHelper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ApiService(funcCode = "AnalysisXwtj", title = "学位调剂大屏接口")
public class AnalysisXwtjIntegrationImpl implements AnalysisXwtjIntegration {
    @Autowired
    EduSjtj03Service eduSjtj03Service;
    @Autowired
    EduHelper eduHelper;
    @Autowired
    CommonIntegration commonIntegration;
    @Autowired
    EduAreaService eduAreaService;
    @Autowired
    EduSchoolService eduSchoolService;
    @Autowired
    EduAddressSchoolService eduAddressSchoolService;
    @Autowired
    EduSchoolTjService eduSchoolTjService;
    @Autowired
    EduStudentEnrollService eduStudentEnrollService;
    @Autowired
    EduSchoolConfigService eduSchoolConfigService;
    @Override
    public XwtjOverViewVO getXwtjInfo(XwtjOverViewQueryVO vo) {
        if(ObjectUtils.isEmpty(vo.getYear())){
            vo.setYear(eduHelper.thisYear());
        }
        if(ObjectUtils.isEmpty(vo.getAreaCode())){
            CommonVO commonVO=new CommonVO();
            commonVO.setParamKey("area_code");
            String areaCode=commonIntegration.getValueByKey(commonVO);
            vo.setAreaCode(areaCode);
        }
        XwtjOverViewVO xwtjOverViewVO=new XwtjOverViewVO();
        xwtjOverViewVO.setYear(vo.getYear());
        xwtjOverViewVO.setAreaCode(vo.getAreaCode());
        //总录取学生
        QueryWrapper<EduSjtj03> enrollStudentWrapper=new QueryWrapper<>();
        enrollStudentWrapper.select("sum(target_num) as enrollStudent").eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0059");
        Map<String,Object> enrollStudentMap=eduSjtj03Service.getMap(enrollStudentWrapper);
        if(!ObjectUtils.isEmpty(enrollStudentMap)){
            BigDecimal bigDecimal= (BigDecimal) enrollStudentMap.get("enrollStudent");
            xwtjOverViewVO.setEnrollStudent(bigDecimal.intValue());
        }else{
            xwtjOverViewVO.setEnrollStudent(0);
        }
        //小学生录取数
        QueryWrapper<EduSjtj03> enrollPrimaryStudentWrapper=new QueryWrapper<>();
        enrollPrimaryStudentWrapper.select("sum(target_num) as enrollPrimaryStudent").eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0060");
        Map<String,Object> enrollPrimaryStudentMap=eduSjtj03Service.getMap(enrollPrimaryStudentWrapper);
        if(!ObjectUtils.isEmpty(enrollPrimaryStudentMap)){
            BigDecimal bigDecimal= (BigDecimal) enrollPrimaryStudentMap.get("enrollPrimaryStudent");
            xwtjOverViewVO.setEnrollPrimaryStudent(bigDecimal.intValue());
        }else{
            xwtjOverViewVO.setEnrollPrimaryStudent(0);
        }
        //中学生录取
        QueryWrapper<EduSjtj03> enrollMiddleStudentWrapper=new QueryWrapper<>();
        enrollMiddleStudentWrapper.select("sum(target_num) as enrollMiddleStudent").eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0061");
        Map<String,Object> enrollMiddleStudentMap=eduSjtj03Service.getMap(enrollMiddleStudentWrapper);
        if(!ObjectUtils.isEmpty(enrollMiddleStudentMap)){
            BigDecimal bigDecimal= (BigDecimal) enrollMiddleStudentMap.get("enrollMiddleStudent");
            xwtjOverViewVO.setEnrollMiddleStudent(bigDecimal.intValue());
        }else{
            xwtjOverViewVO.setEnrollMiddleStudent(0);
        }
        //未录取总数
        QueryWrapper<EduSjtj03> rejectedStudentWrapper=new QueryWrapper<>();
        rejectedStudentWrapper.select("sum(target_num) as rejectedStudent").eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0062");
        Map<String,Object> rejectedStudentMap=eduSjtj03Service.getMap(rejectedStudentWrapper);
        if(!ObjectUtils.isEmpty(rejectedStudentMap)){
            BigDecimal bigDecimal= (BigDecimal) rejectedStudentMap.get("rejectedStudent");
            xwtjOverViewVO.setRejectedStudent(bigDecimal.intValue());
        }else{
            xwtjOverViewVO.setRejectedStudent(0);
        }
        //未录取小学生总数
        QueryWrapper<EduSjtj03> rejectedPrimaryStudentWrapper=new QueryWrapper<>();
        rejectedPrimaryStudentWrapper.select("sum(target_num) as rejectedPrimaryStudent").eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0063");
        Map<String,Object> rejectedPrimaryStudentMap=eduSjtj03Service.getMap(rejectedPrimaryStudentWrapper);
        if(!ObjectUtils.isEmpty(rejectedPrimaryStudentMap)){
            BigDecimal bigDecimal= (BigDecimal) rejectedPrimaryStudentMap.get("rejectedPrimaryStudent");
            xwtjOverViewVO.setRejectedPrimaryStudent(bigDecimal.intValue());
        }else{
            xwtjOverViewVO.setRejectedPrimaryStudent(0);
        }
        //未录取中学生总数
        QueryWrapper<EduSjtj03> rejectedMiddleStudentWrapper=new QueryWrapper<>();
        rejectedMiddleStudentWrapper.select("sum(target_num) as rejectedMiddleStudent").eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0064");
        Map<String,Object> rejectedMiddleStudentMap=eduSjtj03Service.getMap(rejectedMiddleStudentWrapper);
        if(!ObjectUtils.isEmpty(rejectedMiddleStudentMap)){
            BigDecimal bigDecimal= (BigDecimal) rejectedMiddleStudentMap.get("rejectedMiddleStudent");
            xwtjOverViewVO.setRejectedMiddleStudent(bigDecimal.intValue());
        }else{
            xwtjOverViewVO.setRejectedMiddleStudent(0);
        }
        //A类录取率
        QueryWrapper<EduSjtj03> AWrapper=new QueryWrapper<>();
        AWrapper.select("sum(target_num) as A").eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0065");
        Map<String,Object> AMap=eduSjtj03Service.getMap(AWrapper);
        if(!ObjectUtils.isEmpty(AMap)){
            BigDecimal bigDecimal= (BigDecimal) AMap.get("A");
            xwtjOverViewVO.setA(bigDecimal.intValue());
        }else{
            xwtjOverViewVO.setA(0);
        }
        //B类录取率
        QueryWrapper<EduSjtj03> BWrapper=new QueryWrapper<>();
        BWrapper.select("sum(target_num) as B").eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0066");
        Map<String,Object> BMap=eduSjtj03Service.getMap(BWrapper);
        if(!ObjectUtils.isEmpty(BMap)){
            BigDecimal bigDecimal= (BigDecimal) BMap.get("B");
            xwtjOverViewVO.setB(bigDecimal.intValue());
        }else{
            xwtjOverViewVO.setB(0);
        }
        //C类录取率
        QueryWrapper<EduSjtj03> CWrapper=new QueryWrapper<>();
        CWrapper.select("sum(target_num) as C").eq("year",vo.getYear()).eq("Area_Code",vo.getAreaCode()).eq("Target_Code","Y0067");
        Map<String,Object> CMap=eduSjtj03Service.getMap(CWrapper);
        if(!ObjectUtils.isEmpty(CMap)){
            BigDecimal bigDecimal= (BigDecimal) CMap.get("C");
            xwtjOverViewVO.setC(bigDecimal.intValue());
        }else{
            xwtjOverViewVO.setC(0);
        }
        return xwtjOverViewVO;
    }

    @Override
    public List<StuEnrollXxtjVO> getStuEnrollXxtjInfo(StuEnrollXxtjQueryVO vo) {
        List<StuEnrollXxtjVO> stuEnrollXxtjVOS=eduSjtj03Service.getStuEnrollXxtjInfo(vo);
        Integer[] arr={1};
        stuEnrollXxtjVOS=stuEnrollXxtjVOS.stream().peek(e->e.setNum(arr[0]++)).collect(Collectors.toList());
        return stuEnrollXxtjVOS;
    }

    @Override
    public List<SchEnrollXxtjVO> getSchEnrollXxtjInfo(SchEnrollXxtjQueryVO vo) {
        if(ObjectUtils.isEmpty(vo.getYear())){
            vo.setYear(eduHelper.thisYear());
        }
        if(ObjectUtils.isEmpty(vo.getAreaCode())){
            CommonVO commonVO=new CommonVO();
            commonVO.setParamKey("area_code");
            String areaCode=commonIntegration.getValueByKey(commonVO);
            vo.setAreaCode(areaCode);
        }
        List<SchEnrollXxtjVO> schEnrollXxtjVOS=eduSjtj03Service.getSchEnrollXxtjInfo(vo);
        Integer[] arr={1};
        schEnrollXxtjVOS=schEnrollXxtjVOS.stream().peek(e->e.setNum(arr[0]++)).collect(Collectors.toList());
        return schEnrollXxtjVOS.subList(0,Math.min(schEnrollXxtjVOS.size(),20));
    }

    @Override
    public Map<String, Object> getSchEnrollXxtjDetail(StuEnrollDgxxInVO vo) {
        Map<String,Object> result=new HashMap<>();
        result.put("list",eduSjtj03Service.getSchoolInfo(vo));
        EduSchoolVO school = eduSchoolService.getDetail(vo.getSchoolId());
        if(!ObjectUtils.isEmpty(school)){
            result.put("schoolNature",school.getSchoolNature());
            result.put("schoolName",school.getSchoolName());
            result.put("schoolCategory",school.getSchoolCategory());
        }
        SchoolAddressQueryVO saqvo = new SchoolAddressQueryVO();
        saqvo.setSchoolId(vo.getSchoolId());
        List<EduAddressVO> scaddressList =  eduAddressSchoolService.getAddress(saqvo);
        if(!CollectionUtils.isEmpty(scaddressList)){
            result.put("addressName",scaddressList.get(0).getAddressName());
        }
        //待调剂人数
        result.put("dtjrs",eduSchoolTjService.count(new QueryWrapper<EduSchoolTj>().eq("from_school_id",vo.getSchoolId()).eq("year",vo.getYear()).eq("area_code",vo.getAreaCode())));
        //已录取
        Long ylq=eduStudentEnrollService.count(new QueryWrapper<EduStudentEnroll>().eq("enroll_status","2").eq("from_school_id",vo.getSchoolId()).eq("year",vo.getYear()).eq("area_code",vo.getAreaCode()));
        result.put("ylq",ylq);
        //计划招生人数
        QueryWrapper<EduSchoolConfig> qwcon = new QueryWrapper<>();
        qwcon.select("sum(enrolment) enrolment")
                .eq("del","0")
                .eq("year",vo.getYear())
                .eq("school_id",vo.getSchoolId())
                .in("school_category",2,3)
                .eq("area_code",vo.getAreaCode());
        Map<String, Object> map = eduSchoolConfigService.getMap(qwcon);
        if (!ObjectUtils.isEmpty(map)) {
            result.put("zsjh",map.get("enrolment").toString());
        }else{
            result.put("zsjh",0);
        }
        return result;
    }
}
